package vns.client.presenter;

import java.util.ArrayList;
import java.util.List;

import vns.client.VNSmartUI;
import vns.client.event.ObjectTypeEditEvent;
import vns.client.helper.RPCCall;
import vns.client.service.CalendarAllServiceAsync;
import vns.client.service.ObjectTypeServiceAsync;
import vns.client.view.CalendarVPanelUI;
import vns.client.view.ObjectTypeUI;
import vns.shared.CalendarEventEntryDTO;
import vns.shared.ObjectTypeDTO;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class CalendarVPanelPresenter implements Presenter{

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
		doInitTable();
	}

	public interface Display{
		Button getLoadButton();
		
		Widget asWidget();
		void render();
	}

	private final Display display;
	private final SimpleEventBus eventBus;
	private final CalendarAllServiceAsync rpcService;
	
	/*
	 * Public method
	 */
	
	public CalendarVPanelPresenter(CalendarVPanelUI view, SimpleEventBus eventBus, CalendarAllServiceAsync model){
		this.display = view;
		this.eventBus = eventBus;
		this.rpcService= model;
		bind();
	}
	
	/*
	 * Private method
	 */
	
	private boolean bind(){
		display.getLoadButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Window.alert("Calendar: Click");
				doLoadEvent();
			}
		});
		return false;
	}
	
	protected void doLoadEvent() {
		new RPCCall<List<CalendarEventEntryDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<CalendarEventEntryDTO> result) {
				for(CalendarEventEntryDTO dto : result){
					Window.alert("Calendar:" + dto.getTitle());
				}
			}

			@Override
			protected void callService(AsyncCallback<List<CalendarEventEntryDTO>> cb) {
				rpcService.loadEvenFeed(cb);
			}
		}.retry(3);
	}

	private void doInitTable() {

	}

	private void doUpdate(){

	}
}
